package nova

import (
	"context"

	"git.100tal.com/wangxiao_dahai_backend/arch-nova/middleware"
	"git.100tal.com/wangxiao_dahai_backend/arch-nova/rabbitmq"
	"git.100tal.com/wangxiao_dahai_backend/arch-nova/redis"
	"github.com/gin-gonic/gin"
)

var (
	// Http
	HttpEngine *NvHttpEngine
	// Mysql
	MysqlEngine *NvMysqlClient
	// Redis
	RedisEngine *redis.NvRedisClient
	// RabbitMQ
	RabbitMQ *rabbitmq.MqWrapper
	// MysqlCluster
	MysqlCluster NvMysqlCluster
)

// 接口开启访问日志
func AccessLogHandler() gin.HandlerFunc {
	return middleware.HandlerAccessLog()
}

// 接口开启验证token
func TokenValidateHandler() gin.HandlerFunc {
	middleware.SetK12PassportAddr(GetRemoteAddr(APP_PASSPORT) + "/api/passport/token/validate")
	middleware.SetFtPassportAddr(GetRemoteAddr(AppFisBusinessUser) + "/api/buc/v2/token/validate")

	return middleware.HandlerTokenValidate()
}

// 接口开启产品线验证
func BizProductLineHandler() gin.HandlerFunc {
	return middleware.HandlerBizProductLine()
}

// 获取用户id
func GetUserId(ctx context.Context) int {
	if ctx != nil {
		if c, ok := ctx.(*gin.Context); ok {
			return middleware.GetCtxParamInt(c, middleware.NvCtxUserId)
		}
	}

	return 0
}

// 获取token
func GetAccessToken(ctx context.Context) string {
	if ctx != nil {
		if c, ok := ctx.(*gin.Context); ok {
			return middleware.GetCtxParamString(c, middleware.NvCtxAccessToken)
		}
	}

	return ""
}

// 获取请求URI
func GetRequestURI(ctx context.Context) string {
	if ctx != nil {
		if c, ok := ctx.(*gin.Context); ok {
			return c.Request.RequestURI
		}
	}

	return ""
}

// 获取请求body
func GetRequestBody(ctx context.Context) string {
	if ctx != nil {
		if c, ok := ctx.(*gin.Context); ok {
			return middleware.GetCtxParamString(c, middleware.NvCtxRequestBody)
		}
	}

	return ""
}

// 获取AccessToken
func GetDahaiAccessToken(c *gin.Context) string {
	return middleware.GetDahaiAccessToken(c)
}

// 获取产品线
func GetBizProductLine(c *gin.Context) string {
	return middleware.GetBizProductLine(c)
}

// 获取产品线
func GetBizProductLineId(c *gin.Context) int {
	return middleware.GetCtxParamInt(c, middleware.NvCtxBizProductLine)
}
